<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>正在处理登陆</title>
</head>
<body>
<!-- 登陆信息处理 -->
<%
	//读取cookie
	String user = new String();
	Cookie[] cookie=request.getCookies();
	if(cookie!=null){
		for(int i=0;i<cookie.length;i++){
			if(cookie[i].getName().equals("SilverCouldUserName")){
				user=cookie[i].getValue();
			}
		}
	}
    if(user.length()==0){
    	String username=request.getParameter("username");
    	String password=request.getParameter("password");
    	//判断密码是否正确，如果正确，写入cookie，不正确则显示提示信息
    	Class.forName("com.mysql.jdbc.Driver");
    	Connection con=DriverManager.getConnection((String) application.getAttribute("dburl"),(String) application.getAttribute("username"),(String) application.getAttribute("password"));
    	String sqls="SELECT password FROM user WHERE name="+"\'"+username+"\'";
    	Statement sm=con.createStatement();
    	ResultSet rs=sm.executeQuery(sqls);
    	if(rs.last()==false){
    		out.println("没有这个用户，您可以重新登录：<br/>");
    		out.println("<a href=\"index.jsp\">重新登陆</a>");
    		return ;
    	}else{
    		rs.beforeFirst();
    		rs.next();
    		String keys=rs.getString(1);
    		if(keys.equals(password)==false){
    			out.println("密码错误，您可以重新登录：<br/>");
    			out.println("<a href=\"index.jsp\">重新登陆</a>");
    			return ;
    		}
			Cookie c = new Cookie("SilverCouldUserName", username);
			c.setMaxAge(Integer.parseInt(request.getParameter("time"))); // 设置Cookie的存活时间
			c.setPath("/SilverCould/");
			response.addCookie(c); // 写入客户端硬盘
			//return ;
			session.setAttribute("username", username);
			response.sendRedirect("main.jsp");
		}

	} else {
		String username = user;
		response.sendRedirect("main.jsp");
	}
%>
</body>
</html>